var iFDU = require('../../utils/iFDU.js');
var app = getApp();
Page({

  data: {
    config: app.config,
    globalData: app.globalData,
    code: '',
    errMsg: {
      1: '充值码有误',
      2: '充值码已使用',
      3: '充值码已过期',
      4: '该充值码只能体验一次'
    },

    amount: 0,
    showModal: false,
  },

  onLoad: function (options) {
  
  },

  inputCode: function (e) {
    var val = e.detail.value;
    if (val) {
      this.setData({
        code: val.toUpperCase()
      });
    } else {
      this.setData({
        code: ''
      });
    }
  },

  recharge: function (e) {
    var that = this;
    if (!that.data.code) {
      iFDU.toast.showToast({
        title: '请输入充值码',
        mask: false
      })
      return;
    }
    var formId = e.detail.formId;
    iFDU.api.doGet(
      'card/checkCard',
      {
        userId: that.data.globalData.userId,
        code: that.data.code,
        rnd: Math.random()
      },
      function (data) {
        let result = data['result'];
        if (result == 0) {
          that.setData({
            showModal: true,
            formId: formId,
            amount: data['amount']
          });
        } else {
          iFDU.toast.showToast({
            title: that.data.errMsg[result],
            mask: false
          })
        }
      }
    )
  },

  confirmRecharge: function (e) {
    var that = this;
    iFDU.api.doGet(
      'card/rechargeCard',
      {
        userId: that.data.globalData.userId,
        code: that.data.code,
        formId: that.data.formId,
        rnd: Math.random()
      },
      function (data) {
        let result = data['result'];
        if (result == 0) {
          wx.redirectTo({
            url: '../recharge-success/recharge-success',
          })
        } else {
          iFDU.toast.showToast({
            title: that.data.errMsg[result],
            mask: false
          })
        }
      }
    )
  },

  hideModal: function () {
    this.setData({
      showModal: false
    })
  },

  onShareAppMessage: function (res) {
    var that = this;
    return {
      title: '逸见服饰 | 充值码快速入口',
      imageUrl: 'http://s.i-fdu.com/runto/wxa/s/yjshare.jpeg',
      path: 'pages/recharge-code/recharge-code',
      success: function (res) {
      },
      fail: function (res) {
      }
    }
  }
})